package cqrtplm.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "webapi.sap")
@Getter
@Setter
public class SapApiConfig {
    // 直接映射 factory 下的所有属性  注意字段名必须和配置文件里面一致 factory
    private Map<String, FactoryConfig> factory;

    @Getter
    @Setter
    public static class FactoryConfig {
        private String url;
        private String userName;
        private String password;

        /**
         * 安全获取密码（避免内存中明文暴露）
         */
        public char[] getPasswordAsChars() {
            return password != null ? password.toCharArray() : new char[0];
        }
    }

    public FactoryConfig getConfigForFactory(String factoryId) {
        if (factory == null || !factory.containsKey(factoryId)) {
            throw new IllegalArgumentException("未找到工厂 " + factoryId + " 的配置");
        }
        return factory.get(factoryId);
    }
}